package cn.openea.perpartner.crm.web.mapper;

import cn.openea.perpartner.crm.web.entity.CrmRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 角色信息表(CrmRole)表数据库访问层
 *
 * @author Weird077
 * @since 2022-12-19 22:30:36
 */
@Repository
public interface CrmRoleMapper extends BaseMapper<CrmRole> {
    String SELECT_ROLE_VO = " select distinct r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.menu_check_strictly, r.dept_check_strictly," +
            " r.status, r.del_flag, r.create_time, r.remark " +
            " from crm_role r" +
            " left join crm_user_role ur on ur.role_id = r.role_id" +
            " left join crm_user u on u.user_id = ur.user_id";

    @Select(SELECT_ROLE_VO + " ${ew.customSqlSegment}")
    List<CrmRole> selectRoleList(@Param("ew") QueryWrapper<CrmRole> wrapper);

    @Select(SELECT_ROLE_VO + " WHERE r.del_flag = '0' and ur.user_id = #{userId}")
    List<CrmRole> selectRolePermissionByUserId(String userId);

    @Select("select r.role_id" +
            "        from crm_role r" +
            "        left join crm_user_role ur on ur.role_id = r.role_id" +
            "        left join crm_user u on u.user_id = ur.user_id" +
            "    where u.user_id = #{userId}")
    List<Long> selectRoleListByUserId(String userId);

    @Select(SELECT_ROLE_VO + " where r.role_id = #{roleId}")
    CrmRole selectRoleById(Long roleId);

    @Select(SELECT_ROLE_VO + " where r.role_name=#{roleName} and r.del_flag = '0' limit 1")
    CrmRole checkRoleNameUnique(String roleName);
    @Select(SELECT_ROLE_VO + " where r.role_key=#{roleKey} and r.del_flag = '0' limit 1")
    CrmRole checkRoleKeyUnique(String roleKey);
}

